import { reactive, markRaw, computed } from 'vue'
import { store } from '@/store'
import { removeUndefinedParams } from '@/utils/index'
import type { CTableProps } from '@/components/CTable'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import { getReductionList, getFactoryList } from '@/apis/taa/target/reduction'

export const useTableList = () => {
  const rootEnterpriseFlag = computed(() => {
    return store.state.user.userInfo.rootEnterpriseFlag
  })

  let factoryName = ''

  const searchForm = reactive({
    factoryName: rootEnterpriseFlag.value ? '' : factoryName,
    projectTime: rootEnterpriseFlag.value ? new Date().getFullYear().toString() : ''
  })
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '工厂',
      field: 'factoryName',
      type: 'select',
      loadData: (): Promise<any> => {
        return getFactoryList().then((res) => {
          console.log('res', res)
          factoryName = res[0]
          const ans = res.map((item: any) => {
            return {
              label: item,
              value: item
            }
          })
          return ans
        })
      }
    },
    {
      name: '项目年份',
      field: 'projectTime',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getReductionList(params)
  }

  // 处理参数
  const handleParams = () => {
    // 参数
    let params = {
      factoryName: searchForm?.factoryName,
      projectTime: searchForm?.projectTime
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '工厂名称/项目编号',
      field: 'factoryName',
      slot: 'factoryName',
      minWidth: 200,
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '项目时间',
      field: 'projectTime',
      slot: 'projectTime',
      minWidth: 130,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '项目减排量（t）',
      field: 'coReduction',
      slot: 'reduction',
      minWidth: 130,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '项目资料',
      field: 'file',
      slot: 'file',
      minWidth: 130,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人/创建时间',
      field: 'creator',
      slot: 'creator',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 150,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    searchForm,
    searchOptions,
    tableOptions,
    tableFieldsSetting
  }
}
